// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.mid = this.node.getChildByName("mid");
        this.left = this.node.getChildByName("left");
        this.right = this.node.getChildByName("right");
        this.up = this.node.getChildByName("up");
        this.down = this.node.getChildByName("down");
    },

    /* 判断是否跳跃成功
        worldPosition:落点坐标（世界坐标）
        direct：方向，1 代表往右跳；-1代表往左跳
     */
    isJumpSuccess (worldPosition, direct) {
        var midPosition = this.mid.convertToWorldSpaceAR(cc.p(0,0));
        var nearPosition = midPosition;
        if (direct === 1) {
            // 往右跳
            var upPosition = this.up.convertToWorldSpaceAR(cc.p(0,0));
            if (cc.pDistance(nearPosition, worldPosition) > cc.pDistance(upPosition, worldPosition)) {
                nearPosition = upPosition;
            }
            var downPosition = this.down.convertToWorldSpaceAR(cc.p(0,0));
            if (cc.pDistance(nearPosition, worldPosition) > cc.pDistance(downPosition, worldPosition)) {
                nearPosition = downPosition;
            }
        } else {
            // 往左跳
            var leftPosition = this.left.convertToWorldSpaceAR(cc.p(0,0));
            if (cc.pDistance(nearPosition, worldPosition) > cc.pDistance(leftPosition, worldPosition)) {
                nearPosition = leftPosition;
            }
            var rightPosition = this.right.convertToWorldSpaceAR(cc.p(0,0));
            if (cc.pDistance(nearPosition, worldPosition) > cc.pDistance(rightPosition, worldPosition)) {
                nearPosition = rightPosition;
            }
        }
        // 找到最近的点
        if (cc.pDistance(nearPosition, worldPosition) < 100) {
            // 跳跃成功
            worldPosition.x = nearPosition.x;
            worldPosition.y = nearPosition.y;
            return true;
        }
        // 跳跃失败
        return false;
    },

    // update (dt) {},
});
